from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Supplier

def CountSupplier():
    count_supplier_screen = Tk()
    count_supplier_screen.title('统计供应商信息')
    screen_width, screen_height = count_supplier_screen.maxsize()
    # 窗口的宽和高
    width = 915
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    count_supplier_screen.geometry(centre)
    count_supplier_screen.resizable(0, 0)

    supplierr = tablee.session.query(Supplier).all()
    data = Supplier.to_json(supplierr)
    length = len(data)

    tree = ttk.Treeview(count_supplier_screen)  # 创建表格对象
    tree["columns"] = ("编号", "名称", "简称", "地址", "电话", "邮箱", "联系人", "联系人电话", "备注")
    tree.column("编号", width=50)  # 设置列
    tree.column("名称", width=110)
    tree.column("简称", width=60)
    tree.column("地址", width=100)
    tree.column("电话", width=100)
    tree.column("邮箱", width=100)
    tree.column("联系人", width=50)
    tree.column("联系人电话", width=100)
    tree.column("备注", width=50)
    tree.heading("编号", text="编号")  # 显示表头名
    tree.heading("名称", text="名称")
    tree.heading("简称", text="简称")
    tree.heading("地址", text="地址")
    tree.heading("电话", text="电话")
    tree.heading("邮箱", text="邮箱")
    tree.heading("联系人", text="联系人")
    tree.heading("联系人电话", text="联系人电话")
    tree.heading("备注", text="备注")

    j = length - 1

    for i in range(0, length):
        num = data[j]['Sno']
        allname = data[j]['Sallname']
        name = data[j]['Sname']
        address = data[j]['Saddress']
        phone = data[j]['Sphone']
        email = data[j]['Semail']
        contact = data[j]['Scontact']
        conphone = data[j]['Sconphone']
        remarks = data[j]['Sremarks']
        tree.insert("", 0, text='', value=(num, allname, name, address, phone, email, contact, conphone, remarks))
        j = j - 1

    tree.pack()

    tablee.session.close()

    count_supplier_screen.mainloop()


if __name__ == '__main__':
    CountSupplier()